package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.model.homePage.adapter.GridViewAdapter;
import com.shengde.jinwawaforleader.utils.LGImgCompressor;
import com.shengde.jinwawaforleader.utils.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * 学生作品
 * Created by JYQ on 2017/1/9 0009.
 */
public class EditStudentProduction extends BaseActivity {
	private GridViewAdapter adapter;
	private ImageView iv_add_pic;
	private TextView tv_back;
	private TextView tv_title;
	private TextView fill_add;
	private GridView mGridView;
	final ArrayList mDatas = new ArrayList<>();

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.activity_edit_student_production);
		//绑定事件接受
		EventBus.getDefault().register(this);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		fill_add = (TextView) findViewById(R.id.fill_add);
		iv_add_pic = (ImageView) findViewById(R.id.iv_add_pic);
		mGridView = (GridView) findViewById(R.id.gv_pic);

	}

	@Override
	public void initData() {


	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		iv_add_pic.setOnClickListener(this);
	}

	/**
	 * eventBus 回调
	 * @param event
	 */
	@Subscribe
	public void onMessageEvent(MessageEvent event) {
		if (event.message.equals(MessageEvent.SELECT_LOCAL_PHOTO_OK)) {
			if (event.arrayList.size() != 0) {
				iv_add_pic.setVisibility(View.GONE);
			}
			for (int i = 0; i < event.arrayList.size(); i++) {
				Bitmap bitmap = LGImgCompressor.getNativeImage(event.arrayList.get(i));
				mDatas.add(bitmap);
			}
			if (mDatas.size()>10){
				Toast.makeText(this, "图片不能多于9张", Toast.LENGTH_SHORT).show();
				return;
			}
			adapter = new GridViewAdapter(EditStudentProduction.this, mDatas);
			mGridView.setAdapter(adapter);
			mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
					if (position == parent.getChildCount() - 1) {
						Intent intent = new Intent(EditStudentProduction.this, LaocalAlbumsActivity.class);
						startActivity(intent);
					}

				}
			});
		}
	}



	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.iv_add_pic:
				Intent intent = new Intent(this, LaocalAlbumsActivity.class);
				startActivity(intent);
				break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);

	}
}
